package com.demo.order_service.controller;

import java.util.List;

import com.demo.order_service.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.demo.order_service.model.entity.Order;
import com.demo.order_service.model.result.ResultEntity;
import com.demo.order_service.service.OrderService;
import com.github.pagehelper.PageInfo;

/** * @author 付嘉星 
/** * ٩(*Ӧ)و写的全对
/** * 困到昏厥(✽ ﾟдﾟ ✽)  
/** * @date 创建时间：2018年4月19日 上午11:08:39 
/** * @version 1.0 */

@RestController("/")
public class OrderController {
	private OrderService orderService;

    public OrderController(@Autowired OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 分页获取列表
     * @param pageNum
     * @return
     */
    @GetMapping("orderList") //FIXME 换成返回ResultEntity
    public ResponseEntity<PageInfo> getList(@RequestParam(required = false, defaultValue = "1") int pageNum) {
        PageInfo<?> pageInfo = orderService.getOrderByPage(pageNum);
        return new ResponseEntity<>(pageInfo, HttpStatus.OK);
    }
    /**
     *获取所有列表
     * @param 
     * @return
     */
    @GetMapping("getAll")
    public ResponseEntity<List<Order>> getAll() {
        List<Order> order = orderService.getAllOrders();
        return new ResponseEntity<>(order, HttpStatus.OK);   	
    }
    /**
     * 根据id获取详细信息
     * @param id
     * @return
     */
    @GetMapping("getOrder/{oid}") //FIXME 换成返回ResultEntity
    public ResponseEntity<Order> getOrder(@PathVariable Long oid) {
        Order order = orderService.getOrderById(oid);
        return new ResponseEntity<>(order, HttpStatus.OK);   	
    }
    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteById/{oid}")
    public ResponseEntity<ResultEntity> deleteById(@PathVariable Long oid) {
        ResultEntity resultEntity = orderService.deleteOrder(oid);
        return new ResponseEntity<>(resultEntity, resultEntity.getHttpStatus());
    }
    /**
     * 插入订单
     * @return
     */
    @PostMapping("addOrder")
    public ResponseEntity<ResultEntity> addOrder(@RequestBody Order order){
    	ResultEntity resultEntity = orderService.insertOrder(order);
    	return new ResponseEntity<>(resultEntity, resultEntity.getHttpStatus());

    }
    
    /**
     * 修改订单
     * @return
     */
    @PostMapping("updateById/{oid}")
    public ResponseEntity<ResultEntity> updateById(@PathVariable Long oid,Order order) {
    	 ResultEntity resultEntity = orderService.updateById(oid,order);
		return new ResponseEntity<>(resultEntity, resultEntity.getHttpStatus());    	
    }
}
